package com.twu.biblioteca.items;

import com.twu.biblioteca.library.ILibraryItemCollector;

/**
 * @author Alvi Jonathan Sait
 */
public class Book implements ILibraryItem {

    private final String ISBN;
    private final String title;
    private final String author;
    private boolean isAvailable;

    public Book(String ISBN, String title, String author) {
        this.ISBN = ISBN;
        this.title = title;
        this.author = author;
        setAvailable(true);
    }

    public String getTitle() {
        return title;
    }

    public String getISBN() {
        return ISBN;
    }

    public String getAuthor() {
        return author;
    }

    public boolean isAvailable() {
        return isAvailable;
    }

    public void setAvailable(boolean isAvailable) {
        this.isAvailable = isAvailable;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append(ISBN).append(" - ").append(title).append(" by ").append(author);
        return builder.toString();
    }

    @Override
    public void acceptCheckouter(ILibraryItemCollector collector) {
        collector.checkoutBook(this);
    }

    @Override
    public void acceptReturner(ILibraryItemCollector collector) {
        collector.returnBook(this);
    }

    /**
     * Two books are considered equal if they have the same ISBN.
     *
     * @param o - Other object
     * @return <b>true</b> if same ISBN; <b>false</b> otherwise
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Book book = (Book) o;

        return !(ISBN != null ? !ISBN.equals(book.ISBN) : book.ISBN != null);
    }

    @Override
    public int hashCode() {
        return ISBN != null ? ISBN.hashCode() : 0;
    }
}
